/*
 * HttpMessageParser.h
 *
 *  Created on: 2018年7月3日
 *      Author: zhengboyuan
 */

#ifndef HTTPMESSAGEPARSER_H_
#define HTTPMESSAGEPARSER_H_

#include "HttpMessage.h"
#include <functional>
#include "HttpParser.h"
#include <memory>

typedef std::function<void (HttpMessage& msg, std::string& buf)>  HttpMessageHandler;


class HttpMessageParser: public HttpParser
{
public:
    HttpMessageParser();
    virtual ~HttpMessageParser();

    int parse(const char* buffer, size_t size, HttpMessageHandler handler);

    void reset();

protected:
    virtual int onMessageBegin();
    virtual int onUrl(const char*, size_t);
    virtual int onHeader(const std::string& field, const std::string& value);
    virtual int onHeadersComplete();
    virtual int onBody(const char*, size_t);
    virtual int onMessageComplete();
    virtual int onError(int code);

    bool isError() const;

protected:
	std::string m_buffer;
    HttpMessage m_message;

    bool    m_complete;

};

#endif /* HTTPMESSAGEPARSER_H_ */
